package com.wwtech.epp.ui.activity.work.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.SignConfig;
import com.wwtech.epp.entity.TodaySign;
import com.wwtech.epp.ui.activity.work.PunchCardActivity;
import com.wwtech.epp.ui.activity.work.contract.PunchCardContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of PunchCardActivity
 * @date 2017/08/28 16:45:05
 */
public class PunchCardPresenter implements PunchCardContract.PunchCardContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final PunchCardContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private PunchCardActivity mActivity;

    @Inject
    public PunchCardPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull PunchCardContract.View view, PunchCardActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getSignConfig(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getSignConfig(map)
                .subscribe(new Consumer<SignConfig>() {
                    @Override
                    public void accept(SignConfig baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.setSignConfig(baseBack.getData());
                        getSign(new HashMap<String, String>());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
//                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void signUpdate(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.updateSign(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.signBack();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getSign(Map map) {
        Disposable disposable = httpAPIWrapper.getSign(ContainValue.userLogin.getData().getUserId() + "", map)
                .subscribe(new Consumer<TodaySign>() {
                    @Override
                    public void accept(TodaySign baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.setTodaySignInfo(baseBack);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        mView.closeProgressDialog();
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getSignList(Map map) {

    }
}